package com.example.docmanagement.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * 用户额外权限实体，对应 user_permissions 表
 */
@Entity
@Table(name = "user_permissions")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class UserPermission {

    /**
     * 复合主键
     */
    @EmbeddedId
    private UserPermissionId id;

    /**
     * 授权人
     */
    @Column(name = "granted_by")
    private UUID grantedBy;

    /**
     * 授权原因说明
     */
    @Column(name = "grant_reason", columnDefinition = "TEXT")
    private String grantReason;

    /**
     * 过期时间
     */
    @Column(name = "expires_at")
    private LocalDateTime expiresAt;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 复合主键定义
     */
    @Embeddable
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UserPermissionId implements Serializable {
        @Column(name = "user_id")
        private UUID userId;

        @Column(name = "permission_id")
        private Long permissionId;
    }
}


